package com.example.FirstBook.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.FirstBook.adapter.ImageViewpageAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private ViewPager viewpage;
    int[] image = new int[]{R.drawable.a2, R.drawable.a4, R.drawable.ren, R.drawable.hhh, R.drawable.image1};
    List<ImageView> list = new ArrayList<>();
    List<ImageView>iconslist=new ArrayList<>();
    ImageViewpageAdapter adapter;
    private LinearLayout layoyt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        initView();
        //创建ImgaeView到集合
        for (int i = 0; i < image.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setImageResource(image[i]);
            //在代码中写控件，需要手动设置宽高
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);//等比例放大
            list.add(iv);
            //动态生成指示器
            ImageView piv=new ImageView(this);
            piv.setImageResource(R.drawable.point_normal);
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(30,30);
            lp.rightMargin=20;
            piv.setLayoutParams(lp);
            iconslist.add(piv);//添加到集合
            layoyt.addView(piv);//添加到布局
        }
        iconslist.get(0).setImageResource(R.drawable.point_select);
        adapter = new ImageViewpageAdapter(list);
        viewpage.setAdapter(adapter);
        //设置viewpage监听器，监听滑动位置
        viewpage.addOnPageChangeListener(this);
        PollThread pollThread=new PollThread();
        pollThread.start();
    }
    class PollThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        viewpage.setCurrentItem(viewpage.getCurrentItem() + 1);
                    }
                });
            }
        }
    }
    private void initView() {
        viewpage = (ViewPager) findViewById(R.id.viewpage);
        layoyt = (LinearLayout) findViewById(R.id.layoyt);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //那个被选中
    }

    @Override
    public void onPageSelected(int position) {
        //被选中的位置
        for (int i = 0; i < iconslist.size(); i++) {
            iconslist.get(i).setImageResource(R.drawable.point_normal);
        }
        iconslist.get(position%list.size()).setImageResource(R.drawable.point_select);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    }

